<template>
  <!-- 规则定义子页面 分摊动因-->

  <div>
    <el-form
      :model="addRuleAgent"
      ref="addRuleAgent"
      label-position="right"
      size="mini"
      :rules="addRuleAgentRules"
    >
      <el-row>
        <el-col :span="24">
          <h3 style="line-height: 40px; font-weight: bolder; font-size: 13px">
            动因选择
          </h3>
        </el-col>
      </el-row>
      <!-- 动态列 -->
      <div
        style="
          height: 150px;
          overflow: auto;
          overflow-x: hidden;
          margin-bottom: 20px;
        "
      >
        <div
          v-for="(item, index) in addRuleAgent.addRuleInput"
          :key="item.guid"
        >
          <el-row>
            <el-col :span="7">
              <el-form-item
                label-width="80px"
                label="比例："
                :rules="{
                  required: true,
                  pattern: /^\d+$|^\d*\.\d{1,2}$/,
                  message: '请输入数字,小数点最多保留两位',
                  trigger: 'blur',
                }"
                :prop="'addRuleInput.' + index + '.driverP'"
              >
                <el-input
                  :readonly="operateType == '3'"
                  v-model="addRuleAgent.addRuleInput[index].driverP"
                >
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="2">

            </el-col>
            <el-col :span="3">
              <el-link
                :disabled="operateType == '3'"
                @click.native="toOpenAgent(addRuleAgent.addRuleInput[index].driverC)"
                style="margin-left: 50px; line-height: 28px; color: #ff782d"
                ><span style="color: red">*&nbsp;</span>动因</el-link
              >
            </el-col>

            <el-col :span="7">
              <el-form-item
                label=""
                label-width="0px"
                :rules="{
                  required: true,
                  message: '请选择动因',
                  trigger: 'change',
                }"
                :prop="'addRuleInput.' + index + '.driverC'"
              >
                <el-select
                  v-model="addRuleAgent.addRuleInput[index].driverC"
                  :disabled="operateType == '3'"
                >
                  <el-option
                    v-for="item in driverCList"
                    :key="item.driverCode"
                    :label="item.driverName"
                    :value="item.driverCode"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="1">
               <i style="font-size:14px;color:#7297e2;line-height:28px;" class="el-icon-circle-plus-outline" v-if="operateType != '3'"  @click="addRule"
                ></i
              >
            </el-col>
            <el-col :span="2">
               <i
               style="font-size:14px;color:#7297e2;line-height:28px;"
               class="el-icon-delete"
                v-if="operateType != '3'"
                size="small"
                @click="deletRule(index)"
                ></i
              >
            </el-col>
          </el-row>
        </div>
      </div>
      <!--目标和-->
      <el-row>
        <el-col :span="3"> 目标和：{{ totalSumAll }}</el-col>
        <el-col :span="5"
          ><span style="color: red" v-show="totalSumAll > 1"
            >&nbsp;&nbsp;已经超过1，请重新输入</span
          >
        </el-col>
      </el-row>
    </el-form>
    <!-- 动因详情 -->
    <rule-form
      ref="ruleDriverForm"
      :editTheData="editTheData"
      :popupWindowType="popupWindowType"
      :dialogVisibleProps="dialogVisible"
      @getDialogVisible="getDialogVisible"
    ></rule-form>

    <agentDetails
      :toShowAgent="toShowAgent"
      :driverCode="driverCode"
      ref="agentDetails"
    ></agentDetails>
  </div>
</template>
<script>
import { ruleDefinition } from "@/api/index";
import { randomStr } from "@/lib/util.js";
import { publicManage } from "@/api/index";
// 动因的详细页面
import agentDetails from "@/components/ca/agentDetails";

import ruleForm from "@/views/caManage/ruleManage/ruleDriver/ruleDriverFormManage";

export default {
  name: "caAgent",
  components: {
    agentDetails,
    ruleForm,
  },
  props: {
    ruleCode: {
      type: String,
      default: "",
    },
    operateType: {
      type: String,
      default: "",
    },
  },
  data() {
    return {
      driverType: "1",
      //动因详情传入的对象
      editTheData: {},
      //按钮点击类型
      popupWindowType: "",
      //动因详情弹框
      dialogVisible: false,
      driverCode: "",
      toShowAgent: false,
      driverCList: [],
      selectList: [],
      addRuleAgent: {
        addRuleInput: [
          {
            driverPT: "",
            driverP: "",
            driverC: "",
            driverS: "",
            driverO: "",
          },
        ],
      },
      addRuleAgentRules: {
        driverP: [
          { required: true, message: "请输入规则编码", trigger: "blur" },
        ],
        driverC: [{ required: true, message: "请选择动因", trigger: "change" }],
      },
    };
  },
  // 接收从基本信息传递过来的值
  created() {
    this.radioChangeInit();
    this.radioChange();
  },
  mounted() {
    // this.init();
  },
  computed: {
    totalSumAll() {
      let totalSumAll = 0;
     var args = this.addRuleAgent.addRuleInput, // 获取所有参数
      lens = args.length, // 获取参数的长度
      d = 0, // 定义小数位的初始长度，默认为整数，即小数位为0
      sum = 0 
      for(var key in args){
        var str = ""+args[key].driverP;
        if(str.indexOf(".")!= -1){
          // 获取小数长度
          var temp = str.split(".")[1].length;
          // 比较此数的小数位与原小数位的长度，取小数位较长的存储到d中
          d = d < temp ? temp:d
        }
      }
      // 计算需要乘的数值
      var m = Math.pow(10,d);
      for(var key in args){
         if (!isNaN(args[key].driverP)){
           sum += args[key].driverP*m
         } 
      }
      console.log(sum,m,'sum,m')
      return sum/m;
  }
  },
  watch: {
  },
  methods: {
    radioChangeInit() {
      var _self = this;
      this.bus.$on("radioChangeInit", function (val) {
        // 静态比例表
        if (val.driverType == "S") {
          _self.driverType = "0";
          _self.getSelectList();
        }
        // 动态比例表
        if (val.driverType == "D") {
          _self.driverType = "1";
          _self.getSelectList();
        }
      });
    },
    radioChange() {
      var _self = this;
      this.bus.$on("radioChange", function (val) {
        // 静态比例表
        if (val.driverType == "S") {
          _self.driverType = "0";
          _self.addRuleAgent.addRuleInput = [
            {
              driverPT: "",
              driverP: "",
              driverC: "",
              driverS: "",
              driverO: "",
            },
          ];
          _self.getSelectList();
        }
        // 动态比例表
        if (val.driverType == "D") {
          _self.driverType = "1";
          // if(_self.operateType == '1'){
          _self.addRuleAgent.addRuleInput = [
            {
              driverPT: "",
              driverP: "",
              driverC: "",
              driverS: "",
              driverO: "",
            },
          ];
          // }
          _self.getSelectList();
        }
      });
    },

    init() {
      this.getSelectList();
      if (
        this.operateType == "2" ||
        this.operateType == "3" ||
        this.operateType == "5"
      ) {
        this.getRuleAgentDetailToForm();
      }
    },
    // 清除数据
    clearMsg() {
      this.$nextTick(() => {
        this.$refs.addRuleAgent.clearValidate();
      });
      this.addRuleAgent.addRuleInput = [
        {
          driverPT: "",
          driverP: "",
          driverC: "",
          driverS: "",
          driverO: "",
        },
      ];
      this.init();
    },
    getRuleAgentDetailToForm() {
      let params = {
        ruleCode: this.ruleCode,
      };
      ruleDefinition
        .ruleBaseDriverQueryData(params)
        .then((res) => {
          if (res.code == 200) {
            if (res.datas.length > 1) {
              this.addRuleAgent.addRuleInput = res.datas;
              this.addRuleAgent.addRuleInput.splice(
                this.addRuleAgent.addRuleInput.length - 1,
                1
              );
            }
          } else {
            this.$message.error(res.message);
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    getSelectList() {
      let params = {
        driverType: this.driverType,
      };
      ruleDefinition
        .ruleDriverQueryForCombobox(params)
        .then((res) => {
          if (res.code == 200) {
            this.driverCList = res.datas;
          } else {
            this.$message.error(res.message);
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    // 打开动因详情
    toOpenAgent(val) {
      var _self = this;
      if (!val) {
        this.$message.warning("请选择动因");
        return;
      }
      this.dialogVisible = true;
      this.popupWindowType = "3";
      this.editTheData = { driverCode: val };
    },
    addRule() {
      this.addRuleAgent.addRuleInput.push({ guid: randomStr.guid() });
    },
    deletRule(index) {
      if (this.addRuleAgent.addRuleInput.length >= 2) {
        return this.addRuleAgent.addRuleInput.splice(index, 1);
      } else {
        this.$message({
          message: "已是最后一条数据，不能删除",
          type: "warn",
        });
      }
    },
    getDialogVisible(val) {
      this.dialogVisible = val;
    },
  },
};
</script>
<style lang="scss" scoped></style>

